<?php
declare (strict_types = 1);

namespace app\admin\controller\data;

use app\admin\BaseController;
use app\common\model\system\Statement;
use think\facade\Cache;
use app\common\model\data\Garbage AS GDModel;

class Garbage extends BaseController
{
    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['cache','clear','cacheConsole']],
    ];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('index', [
            'cacheSize' => GDModel::getCacheSize(),
            'gbSize'    => GDModel::getGDSize(),
            'relatedSize'=> GDModel::getRelatedSize(),
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function cache()
    {
        if(Cache::clear()){
            return json([
                'code' => 0,
                'msg' => '清空缓存成功.'
            ]);
        }
        return json([
            'code' => 1,
            'msg' => '清空缓存失败,请重试.'
        ]);
    }
    /**
     * 清空控制台缓存
     *
     * @return \think\Response
     */
    public function cacheConsole()
    {
        Statement::chearCache();
        return json([
            'code' => 0,
            'msg' => '清空缓存成功.'
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function clear()
    {
        try{
            GDModel::clearGD();


            return json([
                'code' => 0,
                'msg' => '清理垃圾成功.'
            ]);
        }catch (\Exception $e){
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }

    }

}
